package com.manage.DB;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.manage.handler.UserHandler;

public class UserController {
	// 数据库名称
	private static String DB_NAME = "bugmanage.db";
	// 数据库版本
	private static int DB_VERSION = 2;
	private SQLiteDatabase db;
	private SqliteUserHelper dbSinaHelper;

	// private SqliteHelper dbHelper;
	public UserController(Context context) {
		dbSinaHelper = new SqliteUserHelper(context, DB_NAME, null, DB_VERSION);
		// dbHelper = new SqliteHelper(context, DB_NAME, null, DB_VERSION);
		db = dbSinaHelper.getWritableDatabase();
	}

	public void Close() {
		db.close();
		dbSinaHelper.close();
	}

	// 获取users表中的USER_ID、Access Token、Access Secret的记录
	public List<UserHandler> GetUserList(Boolean isSimple) {
		List<UserHandler> userList = new ArrayList<UserHandler>();
		Cursor cursor = db.query(SqliteUserHelper.TB_NAME, null, null, null, null, null, UserHandler.USER_ID + " DESC");
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && (cursor.getString(1) != null)) {
			UserHandler user = new UserHandler();
			user.setUserId(cursor.getString(0));
			user.setUserName(cursor.getString(1));
			user.setUserEmail(cursor.getString(2));
			user.setUserRealName(cursor.getString(3));
			user.setUserRole(cursor.getString(4));
			user.setUserPassword(cursor.getString(5));

			userList.add(user);
			cursor.moveToNext();
		}
		cursor.close();
		return userList;
	}

	public boolean isNullSinaTb() {
		Boolean flag = false;
		Cursor cursor = db.query(SqliteUserHelper.TB_NAME, null, null, null, null, null, UserHandler.USER_ID + " DESC");
		flag = !cursor.moveToFirst();
		cursor.close();
		return flag;
	}

	// 判断users表中的是否包含某个USER_ID的记录
	public boolean HaveUserInfo(String userId) {
		Boolean b = false;
		Cursor cursor = db.query(SqliteUserHelper.TB_NAME, null, UserHandler.USER_ID + "=" + userId, null, null, null, null);
		b = cursor.moveToFirst();
		// Log.e("HaveUserInfo", b.toString());
		cursor.close();
		return b;
	}

	public final static String USER_ID = "user_id";
	public final static String USER_NAME = "user_name";
	public final static String USER_EMAIL = "user_email";
	public final static String USER_REAL_NAME = "user_real_name";
	public final static String USER_ROLE = "user_role";
	public final static String USER_PASSWORD = "user_password";

	// 更新users表的记录
	public int UpdateUserInfo(UserHandler user, Boolean isSimple) {
		ContentValues values = new ContentValues();
		values.put(UserHandler.USER_ID, user.getUserId());

		int id = db.update(SqliteUserHelper.TB_NAME, values, UserHandler.USER_ID + "=" + user.getUserId(), null);
		// Log.e("UpdateUserInfo", id + "");
		return id;
	}

	// 添加users表的记录
	public Long SaveUserInfo(UserHandler user, Boolean isSimple) {
		ContentValues values = new ContentValues();
		values.put(UserHandler.USER_ID, user.getUserId());
		values.put(UserHandler.USER_NAME, user.getUserName());
		values.put(UserHandler.USER_EMAIL, user.getUserEmail());
		values.put(UserHandler.USER_REAL_NAME, user.getUserRealName());
		values.put(UserHandler.USER_ROLE, user.getUserRole());
		values.put(UserHandler.USER_PASSWORD, user.getUserPassword());

		Long uid = db.insert(SqliteUserHelper.TB_NAME, UserHandler.USER_ID, values);
		// Log.e("SaveUserInfo", uid + "");
		return uid;
	}

	// 删除users表的记录
	public int DelUserInfo(String userId) {
		int id = db.delete(SqliteUserHelper.TB_NAME, UserHandler.USER_ID + "=" + userId, null);
		// Log.e("DelUserInfo", id + "");
		return id;
	}
}
